﻿/**
 * @author sminutoli
 * version 1.0
 * hace temblar por determinada cantidad de tiempo a uno o varios movieclips
 */
import com.dinmax.flash.utilities.timer.Timer
import com.dinmax.flash.utilities.movieclip.MovieClipUtils
import com.dinmax.flash.draw.utilities.BuzzProperties
import com.dinmax.flash.draw.utilities.BuzzPropertiesClassic
class com.dinmax.flash.draw.utilities.Buzz
implements com.dinmax.flash.utilities.timer.TimerListener {
	
	private var clips:Array; //de movieclips
	private var properties:BuzzProperties;
	
	private var mcBuzzWatch:MovieClip;
	private var timer:Timer;
	private var isBuzzing:Boolean;
	
	function Buzz(clip:MovieClip, properties:BuzzProperties) {
		this.removeClips();
		this.addClip(clip);
		if(!properties) properties = new BuzzPropertiesClassic();
		this.setProperties(properties);
		if(!this.timer) { 
			this.timer = new Timer(this.getProperties().getInterval());
			this.timer.addListener(this);
		};
		return;
	};
	
	public function startBuzz():Void {
		this.isBuzzing = true;
		this.buzzClips();
		timer.beginTimer();
		return;
	};
	
	public function endBuzz():Void {
		this.isBuzzing = false;
		this.timer.finishTimer();
		delete mcBuzzWatch.onEnterFrame;
		MovieClipUtils.destroyClip(mcBuzzWatch);
		return;
	};
	
	private function buzzClips():Void {
		var _movieTimer:Number=getTimer();
		var bPair:Boolean= false;
		var root:Buzz = this;
		if(!mcBuzzWatch){
			mcBuzzWatch = _root.createEmptyMovieClip("buzzWatch", _root.getNextHighestDepth());
		}
		
		mcBuzzWatch.onEnterFrame=function(){
			var nDif:Number= getTimer() - _movieTimer;
			if(nDif > root.getProperties().getDuration()){
				delete this.onEnterFrame;
			} else {
				root.desplaceClips(bPair);
				bPair = !bPair;
			};
		};
		return;
	};
	
	private function desplaceClips(positive:Boolean):Void {
		for (var i : Number = 0; i < clips.length; i++) {
			if(positive){
				clips[i]._x += this.getProperties().getXdesp();
				clips[i]._y += this.getProperties().getYdesp();
			} else {
				clips[i]._x -= this.getProperties().getXdesp();
				clips[i]._y -= this.getProperties().getYdesp();
			};	
		}
		return;
	};
	
	public function addClip(clip:MovieClip):Void {
		this.clips.push(clip);
		return;
	};
	public function getClips():Array {
		return this.clips;
	};
	public function removeClips():Void {
		this.clips = new Array();
		return;
	};
		
	public function getIsBuzzing():Boolean {
		return isBuzzing;
	}

	public function getProperties():BuzzProperties {
		return properties;
	}

	public function setProperties(properties:BuzzProperties):Void {
		this.properties = properties;
	}
	
	public function	timeOut():Void {
		this.buzzClips();
		this.timer.beginTimer();
		return;
	};
};